local sz_yanyu = fk.CreateSkill {
    name = "sz_yanyu",
}
Fk:loadTranslationTable {
    ["sz_yanyu"] = "燕语",
    [":sz_yanyu"] = "出牌阶段，你可以重铸至多两倍体力值等量的基本牌；弃牌阶段开始，根据你本回合重铸的基本牌名数，你令一名角色执行前X项(X为重铸的牌名数，若目标为自己，则仅你执行效果)：" ..
        "<br>1.你与其摸的等量的牌；<br>2.你与其回复一点体力，本轮手牌上限+2；" ..
        "<br>3.从随机等量的蜀国男性武将技能中选择一个令其获得直到你的下个出牌阶段结束；<br>4.你与其增加一点体力上限。",

    ["$sz_yanyu1"] = "画堂欢燕飞，情许鸳鸯合",
    ["$sz_yanyu2"] = "欢娱在今夕，燕婉及良时",

    ["#sz_yanyu-ask"] = "燕语：你还可以重铸任意张基本牌，剩余 %arg 张",
    ["#sz_yanyu-chos"] = "燕语：你可以选择一名其他角色，执行前 %arg 项",
    ["@sz_yanyu_skill"] = "燕语",
    ["@$sz_yanyu-turn"] = "燕语",
}
sz_yanyu:addEffect("active", {
    name = "sz_yanyu",
    anim_type = "drawcard",
    min_card_num = 1,
    max_card_num = function(self, player)
        return player.hp * 2 - player:getMark("sz_yanyu-turn")
    end,
    target_num = 0,
    prompt = function(self, player)
        return "#sz_yanyu-ask:::" .. player.hp * 2 - player:getMark("sz_yanyu-turn")
    end,
    target_filter = Util.FalseFunc,
    can_use = function(self, player)
        return player:getMark("sz_yanyu-turn") < player.hp * 2
    end,
    card_filter = function(self, player, to_select, selected)
        return #selected < player.hp * 2 and not player:prohibitDiscard(Fk:getCardById(to_select)) and
            Fk:getCardById(to_select).type == Card.TypeBasic
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        for _, i in ipairs(effect.cards) do
            local card = Fk:getCardById(i)
            if not table.contains(player:getTableMark("@$sz_yanyu-turn"), card.trueName) then
                room:addTableMark(player, "@$sz_yanyu-turn", card.trueName)
            end
        end
        room:recastCard(effect.cards, player, sz_yanyu.name)
        room:addPlayerMark(player, "sz_yanyu-turn", #effect.cards)
    end,
})
sz_yanyu:addEffect(fk.EventPhaseStart, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        local mark = player:getTableMark("@$sz_yanyu-turn")
        return target == player and player:hasSkill(sz_yanyu.name) and player.phase == Player.Discard and #mark > 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local tos = room:askToChoosePlayers(player, {
            targets = room.alive_players,
            min_num = 1,
            max_num = 1,
            prompt = "#sz_yanyu-chos:::" .. #player:getTableMark("@$sz_yanyu-turn"),
            skill_name = sz_yanyu.name,
            cancelable = true
        })
        if #tos > 0 then
            event:setCostData(self, { tos = tos })
            return true
        end
        return false
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("@$sz_yanyu-turn")
        local to = event:getCostData(self).tos[1]
        if #mark > 0 then
            to:drawCards(#mark, sz_yanyu.name)
            if to ~= player then
                player:drawCards(#mark, sz_yanyu.name)
            end
        end
        if #mark > 1 then
            room:recover({
                who = to,
                num = 1,
                recoverBy = player,
                skillName = sz_yanyu.name,
            })
            room:addPlayerMark(to, "AddMaxCards-round", 2)
            if to ~= player then
                room:recover({
                    who = player,
                    num = 1,
                    recoverBy = player,
                    skillName = sz_yanyu.name,
                })
                room:addPlayerMark(player, "AddMaxCards-round", 2)
            end
        end
        if #mark > 2 then
            local generals, same_generals = {}, {}
            for _, general_name in ipairs(room.general_pile) do
                same_generals = Fk:getSameGenerals(general_name)
                table.insert(same_generals, general_name)
                same_generals = table.filter(same_generals, function(g_name)
                    local general = Fk.generals[g_name]
                    return general.kingdom == "shu"
                end)
                if #same_generals > 0 then
                    table.insert(generals, table.random(same_generals))
                end
            end
            if #generals == 0 then return false end
            generals = table.random(generals, #mark)

            local skills = {}
            for _, general_name in ipairs(generals) do
                local general = Fk.generals[general_name]
                local general_skills = {}
                for _, s in ipairs(general:getSkillNameList()) do
                    local skill = Fk.skills[s]
                    if not player:hasSkill(skill) and not (skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) then
                        table.insertIfNeed(general_skills, s)
                    end
                end
                table.insertIfNeed(skills, general_skills)
            end
            if #skills > 0 then
                local choices = U.askToChooseGeneralSkills(player, {
                    generals = generals,
                    skills = skills,
                    min_num = 1,
                    max_num = 1,
                    skill_name = sz_yanyu.name,
                    prompt = " 燕语：请选择一个技能获得",
                    cancelable = false
                })
                room:handleAddLoseSkills(player, table.concat(choices, "|"))
                room:setPlayerMark(to, "@sz_yanyu_skill", choices)
            end
        end
        if #mark > 3 then
            room:changeMaxHp(to, 1)
            if to ~= player then
                room:changeMaxHp(player, 1)
            end
        end
    end,
})
sz_yanyu:addEffect(fk.EventPhaseEnd, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:hasSkill(sz_yanyu.name) and player.phase == Player.Play
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        for _, p in ipairs(room:getAlivePlayers()) do
            if p:getMark("@sz_yanyu_skill") ~= 0 then
                local skills = p:getMark("@sz_yanyu_skill")
                room:setPlayerMark(p, "@sz_yanyu_skill", 0)
                room:handleAddLoseSkills(p, "-" .. table.concat(skills, "|-"))
            end
        end
    end,
})


return sz_yanyu
